package Java.BlackHorse.Thread.Thread4;

public class MyThread extends Thread{
    public MyThread() {
    }

    public MyThread(String name) {
        super(name);
    }

    static Object o = new Object();

    static int ticket = 1;
    @Override
    public void run() {
        while(true){
//            传入的锁对象必须是唯一的
            synchronized(MyThread.class){
                if (ticket <= 100) {
                    try {
                        sleep(100);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println(Thread.currentThread().getName()+" 抢到了第 "+ticket+" 张票 ");
                    ticket++;
                } else {
                    break;
                }
            }
        }
    }
}
